# -*- coding: utf-8 -*-
# @Time         : 2021/5/10 14:19
# @Author       : Jinxing Lin
# @StudentNumber: 20216523
# @Affiliation  : SUN YAT-SEN UNIVERSITY  SCHOOL OF SYSTEMS SCIENCE AND ENGINEERING
# @Mail         ：linjx83@mail2.sysu.edu.cn
# @FileName     : main.py
# @Software     : PyCharm

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


def plot_line_chart2(x_names, data, x_title, y_title1, y_title2, line_name=[], save_name=""):
    '''
        折线图
    :param x_names:横轴名称
    :param data:数据
    :param x_title:横轴标题
    :param y_title:纵轴标题
    :param line_name:折线名称
    :param save_name:图片保存名称
    :return:
    '''
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

    # 属性参数
    font = {"size": 34}
    fig, ax = plt.subplots(figsize=(15, 13))  # 第一个是宽，第二个是高
    marks = ['8', 's', '*', '>', 'p', 'd', 'H', 'o', 'd']
    colors = ["#8C564B", "#92D050", "#00B0F0", "#F59D56", "#BFBFBF",
              "#FFD965", "#C00000", "#CC00FF", "#FF6699"]
    markersize = 15
    tick_font = 35
    x = range(len(x_names))

    # 左侧y轴每个指标画一条线
    for index in range(data.shape[0] - 1):
        plt.plot(x, data[index],
                 markersize=markersize,
                 color=colors[index],
                 marker=marks[index],
                 label=line_name[index])

    plt.xticks(x, x_names, fontsize=tick_font, rotation=90)

    # 设置右侧y轴
    ax2 = ax.twinx()
    index = data.shape[0] - 1
    ax2.plot(x, data[index],
             markersize=markersize,
             color=colors[index],
             marker=marks[index],
             label=line_name[index])

    # y轴刻度
    plt.setp(ax.get_yticklabels(), fontsize=tick_font)
    plt.setp(ax2.get_yticklabels(), fontsize=tick_font)

    # 标题
    ax.set_xlabel(x_title, font, labelpad=15)
    ax.set_ylabel(y_title1, font, color='black')  # 左侧y轴
    ax2.set_ylabel(y_title2, font, color='black')  # 右侧y轴

    # 边框不可见
    # ax.spines['top'].set_visible(False)
    # ax.spines['right'].set_visible(False)

    # 画图例
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width, box.height * 1.0])
    # ax.legend(loc='center left',
    #           bbox_to_anchor=(0, 1.08),
    #           ncol=3,
    #           fontsize=31)
    fig.legend(loc='center left',
               bbox_to_anchor=(0.2, 1.08),
               ncol=2,
               fontsize=31,
               bbox_transform=ax.transAxes)

    # 从0.1开始
    # ax.set_ylim(bottom=1)

    # 以10为底取对数
    # ax.set_yscale("log")
    # ax.semilogy()

    plt.tight_layout()
    plt.savefig('{}.png'.format(save_name), dpi=800, format='png', bbox_inches='tight')
    # plt.show()


# read from result_svm_func.csv
df = pd.read_csv("combine_result3.csv", index_col=0, header=0)
x_names = list(df.columns)
line_name = list(df.index)
data = np.asarray(df)
x_title = "训练集比例"
y_title1 = "度量分类效果指标"
y_title2 = "时间/s"
plot_line_chart2(x_names, data,
                 x_title, y_title1, y_title2,
                 line_name=line_name, save_name="plot_result3")
